package com.EAIPClub.CodeShell.service.impl;

import com.EAIPClub.CodeShell.service.CodeShellDebugService;
import com.api.API.CodeLab.CodeShellConnecter.OrderControllerAPI;
import com.api.POJO.CodeLab.CodeShell.CodeShellDebugInfo;
import com.api.POJO.CodeLab.CodeShell.CodeShellDebugReturnBean;
import com.api.POJO.CodeLab.CodeShell.CodeShellDebugReturnMsg;
import com.api.POJO.CodeLab.CodeShellConnecter.WebSSHCommand;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/19
 */
@Service
@Slf4j
public class CodeShellDebugServiceImpl implements CodeShellDebugService {

    private OrderControllerAPI orderControllerAPI;
    private CodeShellDebugInfo infos =null;

    @Autowired
    public void setOrderController(OrderControllerAPI orderControllerAPI) {
        this.orderControllerAPI = orderControllerAPI;
    }

    @Override
    @SneakyThrows
    public CodeShellDebugReturnMsg runDebug(CodeShellDebugInfo info) {
        CodeShellDebugReturnMsg returnMsg = new CodeShellDebugReturnMsg();
        List<String> Msg = new ArrayList<>();
        infos=info;
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("javac -g "+info.getMainName()+".java"+" & jdb "+info.getMainName()+" | tee "+info.getMainName()+".txt"));
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("stop at "+info.getMainName()+":"+info.getLine()));
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("run"));
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    @SneakyThrows
    public CodeShellDebugReturnMsg next() {
        CodeShellDebugReturnMsg returnMsg = new CodeShellDebugReturnMsg();
        List<String> Msg = new ArrayList<>();
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("next"));
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("locals"));
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("dump "+infos.getArrName()));
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<CodeShellDebugReturnBean> forEntity = restTemplate.getForEntity("http://192.168.122.42:8615/codeshell/readfile/" + infos.getMainName() + ".txt", CodeShellDebugReturnBean.class);
        returnMsg.setData(Objects.requireNonNull(forEntity.getBody()).getData());
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    @SneakyThrows
    public CodeShellDebugReturnMsg showList() {
        CodeShellDebugReturnMsg returnMsg = new CodeShellDebugReturnMsg();
        List<String> Msg = new ArrayList<>();
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("list"));
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    @SneakyThrows
    public CodeShellDebugReturnMsg run() {
        CodeShellDebugReturnMsg returnMsg = new CodeShellDebugReturnMsg();
        List<String> Msg = new ArrayList<>();
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("step up"));
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("locals"));
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("dump "+infos.getArrName()));
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<CodeShellDebugReturnBean> forEntity = restTemplate.getForEntity("http://192.168.122.42:8615/codeshell/readfile/" + infos.getMainName() + ".txt", CodeShellDebugReturnBean.class);
        returnMsg.setData(Objects.requireNonNull(forEntity.getBody()).getData());
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    @SneakyThrows
    public CodeShellDebugReturnMsg exit() {
        CodeShellDebugReturnMsg returnMsg = new CodeShellDebugReturnMsg();
        List<String> Msg = new ArrayList<>();
        orderControllerAPI.sendCommandToSSH(new WebSSHCommand("exit"));
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }
}
